/*
 * 一些经验
 * 1、forEach时灵时不灵
 * 2、for in注意排除非数据
 */
var rule = {
    title:'布米米',
    host:'http://www.bumimi.vip',
    hostJs:'let html=request(HOST,{headers:{"User-Agent":PC_UA}});let src=jsp.pdfh(html,"a:eq(0)&&href");HOST=src.replace(/\\/$/,"")',
    url:'fyclass/fyfilter/fypage.html',
    filter_url:'{{fl.year}}/{{fl.cate}}/{{fl.area}}',
    filter:{'tv':[{'key':'year','name':'年代','value':[{'n':'全部','v':'0'},{'n':'2024','v':'2024'},{'n':'2023','v':'2023'},{'n':'2022','v':'2022'},{'n':'2021','v':'2021'},{'n':'2020','v':'2020'},{'n':'2019','v':'2019'},{'n':'2018','v':'2018'},{'n':'2017','v':'2017'},{'n':'2016','v':'2016'},{'n':'2015','v':'2015'},{'n':'更早','v':'2000'}]},{'key':'cate','name':'类型','value':[{'n':'全部','v':'0'},{'n':'古装','v':'19'},{'n':'言情','v':'20'},{'n':'武侠','v':'21'},{'n':'偶像','v':'22'},{'n':'家庭','v':'23'},{'n':'青春','v':'24'},{'n':'都市','v':'25'},{'n':'爱情','v':'26'},{'n':'喜剧','v':'27'},{'n':'战争','v':'28'},{'n':'军旅','v':'29'},{'n':'谍战','v':'30'},{'n':'悬疑','v':'31'},{'n':'罪案','v':'32'},{'n':'穿越','v':'33'},{'n':'宫廷','v':'34'},{'n':'历史','v':'35'},{'n':'神话','v':'36'},{'n':'科幻','v':'37'},{'n':'年代','v':'38'},{'n':'农村','v':'39'},{'n':'商战','v':'40'},{'n':'剧情','v':'41'},{'n':'奇幻','v':'42'}]},{'key':'area','name':'地区','value':[{'n':'全部','v':'0'},{'n':'国产','v':'china'},{'n':'港剧','v':'hk'},{'n':'台剧','v':'tw'},{'n':'美剧','v':'american'},{'n':'韩剧','v':'korea'},{'n':'泰剧','v':'thailand'},{'n':'日剧','v':'japan'},{'n':'英剧','v':'england'}]}],'mov':[{'key':'year','name':'年代','value':[{'n':'全部','v':'0'},{'n':'2024','v':'2024'},{'n':'2023','v':'2023'},{'n':'2022','v':'2022'},{'n':'2021','v':'2021'},{'n':'2020','v':'2020'},{'n':'2019','v':'2019'},{'n':'2018','v':'2018'},{'n':'2017','v':'2017'},{'n':'2016','v':'2016'},{'n':'2015','v':'2015'},{'n':'更早','v':'2000'}]},{'key':'cate','name':'类型','value':[{'n':'全部','v':'0'},{'n':'喜剧','v':'1'},{'n':'悲剧','v':'2'},{'n':'爱情','v':'3'},{'n':'动作','v':'4'},{'n':'枪战','v':'5'},{'n':'犯罪','v':'6'},{'n':'惊悚','v':'7'},{'n':'恐怖','v':'8'},{'n':'悬疑','v':'9'},{'n':'动画','v':'10'},{'n':'家庭','v':'11'},{'n':'奇幻','v':'12'},{'n':'魔幻','v':'13'},{'n':'科幻','v':'14'},{'n':'战争','v':'15'},{'n':'青春','v':'16'},{'n':'剧情','v':'17'}]},{'key':'area','name':'地区','value':[{'n':'全部','v':'0'},{'n':'国产','v':'china'},{'n':'香港','v':'hk'},{'n':'美国','v':'american'},{'n':'英国','v':'england'},{'n':'韩国','v':'korea'},{'n':'泰国','v':'thailand'},{'n':'日本','v':'japan'},{'n':'印度','v':'india'}]}],'zongyi':[{'key':'year','name':'年代','value':[{'n':'全部','v':'0'},{'n':'2024','v':'2024'},{'n':'2023','v':'2023'},{'n':'2022','v':'2022'},{'n':'2021','v':'2021'},{'n':'2020','v':'2020'},{'n':'2019','v':'2019'},{'n':'2018','v':'2018'},{'n':'2017','v':'2017'},{'n':'2016','v':'2016'},{'n':'2015','v':'2015'},{'n':'更早','v':'2000'}]},{'key':'cate','name':'类型','value':[{'n':'全部','v':'0'},{'n':'播报','v':'43'},{'n':'访谈','v':'44'},{'n':'搞笑','v':'45'},{'n':'游戏','v':'46'},{'n':'选秀','v':'47'},{'n':'时尚','v':'48'},{'n':'情感','v':'49'},{'n':'晚会','v':'50'},{'n':'曲艺','v':'51'},{'n':'美食','v':'52'},{'n':'少儿','v':'53'},{'n':'脱口秀','v':'54'},{'n':'职场','v':'55'},{'n':'相亲','v':'56'},{'n':'音乐','v':'57'},{'n':'伦理','v':'58'},{'n':'真人秀','v':'59'},{'n':'舞蹈','v':'60'},{'n':'亲子','v':'61'},{'n':'财经','v':'62'},{'n':'旅游','v':'63'},{'n':'益智','v':'64'},{'n':'竞技','v':'65'},{'n':'纪实','v':'66'},{'n':'生活','v':'67'},{'n':'盛会','v':'68'},{'n':'歌舞','v':'69'},{'n':'其它','v':'70'}]},{'key':'area','name':'地区','value':[{'n':'全部','v':'0'},{'n':'中国','v':'china'},{'n':'英国','v':'england'},{'n':'美国','v':'american'},{'n':'韩国','v':'korea'},{'n':'泰国','v':'thailand'},{'n':'日本','v':'japan'}]}],'acg':[{'key':'year','name':'年代','value':[{'n':'全部','v':'0'},{'n':'2024','v':'2024'},{'n':'2023','v':'2023'},{'n':'2022','v':'2022'},{'n':'2021','v':'2021'},{'n':'2020','v':'2020'},{'n':'2019','v':'2019'},{'n':'2018','v':'2018'},{'n':'2017','v':'2017'},{'n':'2016','v':'2016'},{'n':'2015','v':'2015'},{'n':'更早','v':'2000'}]},{'key':'cate','name':'类型','value':[{'n':'全部','v':'0'},{'n':'搞笑','v':'71'},{'n':'经典','v':'72'},{'n':'热血','v':'73'},{'n':'催泪','v':'74'},{'n':'治愈','v':'75'},{'n':'猎奇','v':'76'},{'n':'励志','v':'78'},{'n':'战斗','v':'80'},{'n':'后宫','v':'81'},{'n':'机战','v':'82'},{'n':'恋爱','v':'84'},{'n':'百合','v':'85'},{'n':'科幻','v':'86'},{'n':'奇幻','v':'88'},{'n':'推理','v':'89'},{'n':'校园','v':'90'},{'n':'运动','v':'91'},{'n':'魔法','v':'94'},{'n':'历史','v':'95'},{'n':'伪娘','v':'101'},{'n':'美少女','v':'102'},{'n':'萝莉','v':'103'},{'n':'亲子','v':'105'},{'n':'青春','v':'107'},{'n':'冒险','v':'108'},{'n':'竞技','v':'109'}]},{'key':'area','name':'地区','value':[{'n':'全部','v':'0'},{'n':'国产','v':'china'},{'n':'日本','v':'japan'},{'n':'英国','v':'england'},{'n':'美国','v':'american'},{'n':'韩国','v':'korea'}]}]},
    detailUrl:'',
    searchUrl: '/search/**',
    searchable: 2,
    quickSearch: 0,
    filterable:1,//是否启用分类筛选,
    headers:{
        'Accept-Encoding': 'no-zip',
        'User-Agent': 'PC_UA'
    },
    timeout:5000,
    class_name:'电视剧&电影&综艺&动漫',
    class_url:'tv&mov&zongyi&acg',
    pagecount:{'tv':1,'mov':1,'zongyi':1,'acg':1},
    play_parse:true,
    lazy:'',
    limit:6,
    // 列表;标题;图片;描述;链接;详情 其中最后一个参数选填
    推荐:'.mod.mod-new .y-pack;.p-thumb a&&title;.quic&&src;;.p-thumb a&&href',
    一级:`js:
        log('一级 input: ' + input);
        let inputSplit = input.split('/'), originUrl = inputSplit.slice(0,3).join('/');
        inputSplit = inputSplit.map(function(item, idx) {
            if(idx > 3 && idx < 6 && item.length < 1) {
                item = '0'
            } else if(idx == 6 && item.length < 1) {
                item = 'all'
            }
            return item
        });
        let url = inputSplit.join('/');
        log('Fixed url: ' + url);

        let d = [];
        let content = request(url);
        let videoList = jsp.pdfa(content, '.b-listtab-main ul.list>li>a');
        videoList.forEach(function(item) {
            let vod_id = jsp.pdfh(item, 'a:eq(0)&&href');
            if(vod_id.startsWith('/')) vod_id = originUrl+vod_id;
            d.push({
                url:vod_id,
                title:jsp.pdfh(item, 'a:eq(0)&&title'),
                img:jsp.pdfh(item, 'img:eq(0)&&data-img'),
                desc:jsp.pdfh(item, 'span.hint:eq(0)&&Text')
            })
        });

        setResult(d)
    `,
    二级:`js:
        log('二级 input: ' + input);
        function unicode2Chinese(str) {
            let str_split = str.split('%u');
            if(str_split.length > 0) {
                let str_unescape = unescape(str);
                if(str != str_unescape) return str_unescape
            } else {
                return str
            }

            let regex = /^[0-9a-fA-F]{4}/, str_list = [];
            str_split.forEach(function(item, idx) {
                if(idx > 0) {
                    code = item.match(regex)
                    if(code) {
                        str_list.push(String.fromCharCode(parseInt("0x"+code[0]))+item.replace(code[0], ''))
                    } else {
                        str_list.push('%u'+item)
                    }
                } else {
                    str_list.push(item)
                }
            });
            return str_list.join('')
        }

        function getVodXiaoK(input) {
            if(!input.endsWith('/')) input += '/';
            let d = {};
            let inputSplit = input.split('/'), originUrl = inputSplit.slice(0,3).join('/');
            let lines = {'zd':'最大','yj':'永久','hn':'牛牛','gs':'光波','sn':'新朗','wl':'涡轮','lz':'良子','fs':'F速','ff':'飞飞','bd':'百度','uk':'酷U','wj':'无天','bj':'八戒','tk':'天空','ss':'速速','kb':'酷播','sd':'闪电','xk':'看看','tp':'淘淘','jy':'精英','sl':'sl线'};
            //let line_sorts = ['yj','zd','lz','ff','kb','sn','fs','uk','hn','wj','sd','bj','tp','wl','bd','xk','ss','tk','sl'];
            let regex = new RegExp('([^"]+s'+inputSplit[4]+'\\\\.js[^"]*)'),
                regex_line = /var\\s+playarr_([a-z]{2})\\s*=\\s*new\\s+Array/g;

            let content = request(input);
            let info = jsp.pdfh(content, 'div.p-top-main:eq(0)'), type = 1;
            if(info.length < 1) {
                info = jsp.pdfh(content, 'div.content:eq(0)');
                type = 2;
            }

            if(info.length < 1) {
                log('未获取二级详情');
                return d
            }

            let info_list = [], line_url = content.match(regex), vod_play_from = [], vod_play_url = [];
            if(!line_url) {
                log('未获取二级线路url');
                return d
            }
            line_url = line_url[1];
            log('line_url: ' + line_url);
            //let line_sorts = eval(content.match(/var\\s+cars_zyw\\s*=\\s*([^;]*)/)[1]);

            let headers = oheaders;
            headers['Origin'] = originUrl;
            headers['Referer'] = input.replace(inputSplit[4], urlencode(inputSplit[4]));
            let line_content = request(line_url, headers);
            let line_code = regex_line.exec(line_content);
            while(line_code) {
                vod_play_from.push(lines[line_code[1]]);
                let vod_items = [];
                let regex_playurl = new RegExp('playarr_'+line_code[1]+'\\\\[(\\\\d+)\\\\]\\\\s*=\\\\s*"(.*?),[\\\\-\\\\d]+,(.*?)"', 'g');
                let playurl = regex_playurl.exec(line_content);
                while(playurl) {
                    vod_items.push(unicode2Chinese(playurl[3])+'$'+playurl[2]);
                    playurl = regex_playurl.exec(line_content)
                }
                vod_play_url.push(vod_items.join('#'));
                line_code = regex_line.exec(line_content)
            }

            if(1 == type) {
                for(let i=0; i<4; i++) {
                    info_list[i] = jsp.pdfa(info, '.top-info-detail&&div&&p:eq('+i+')&&a').map(function(item, idx) {
                        return jsp.pdfh(item, 'a:eq(0)&&Text')
                    })
                }
                d = {
                    vod_id: input,
                    vod_name: jsp.pdfh(info, 'h1:eq(0)&&Text'),
                    vod_pic: jsp.pdfh(info, '.b-detailcover img:eq(0)&&data-img'),
                    type_name: info_list[0].join('/'),
                    vod_year: info_list[1].join('/'),
                    vod_area: info_list[2].join('/'),
                    vod_remarks: jsp.pdfh(info, '.top-info-title&&p:eq(0)&&Text'),
                    vod_actor: jsp.pdfh(info, '.top-info-detail&&div&&p:eq(4)&&Text').replace(/(主演：|主持人：|演员：)/g, ''),
                    vod_director: info_list[3].join('/'),
                    vod_content: jsp.pdfh(info, '.item-desc:eq(0)&&Text').replace('简介：', ''),
                    vod_play_from: vod_play_from.join('$$$'),
                    vod_play_url: vod_play_url.join('$$$')
                }
            } else {
                let area_year = jsp.pdfh(info, '.info&&dl&&dd:eq(1)&&Text').split(' ');
                let remarks = line_content.match(/lianzaijs_.*?=(.*?);/);
                remarks = remarks ? '更新至'+remarks[1]+'集' : '';
                d = {
                    vod_id: input,
                    vod_name: jsp.pdfh(info, '.pic img:eq(0)&&alt'),
                    vod_pic: jsp.pdfh(info, '.pic img:eq(0)&&data-original'),
                    type_name: jsp.pdfa(info, '.info&&dl&&dd:eq(2)&&a').map(function(item, idx) {
                        return jsp.pdfh(item, 'a:eq(0)&&Text')
                    }).join('/'),
                    vod_year: area_year.length>1?area_year[1].replace('年代：',''):'',
                    vod_area: area_year.length>0?area_year[0].replace('地区：',''):'',
                    vod_remarks: remarks,
                    vod_actor: jsp.pdfh(info, '.info&&dl&&dd:eq(0)&&Text').replace(/(主演：|主持人：|演员：)/g, ''),
                    vod_director: '',
                    vod_content: jsp.pdfh(info, '.des2:eq(0)&&Text').replace('剧情：　　', '').replace('剧情：', ''),
                    vod_play_from: vod_play_from.join('$$$'),
                    vod_play_url: vod_play_url.join('$$$')
                }
            }
            return d
        }

        VOD = getVodXiaoK(input)
    `,
    搜索:`js:
        log('搜索 input: ' + input);
        let d = [], jsondata = [];
        let inputSplit = input.split('/'), originUrl = inputSplit.slice(0,3).join('/');
        let regex = /var my_search='(.*?)'/;
        let headers = oheaders;
        headers['Origin'] = originUrl;
        headers['Referer'] = input.replace(inputSplit[4], urlencode(inputSplit[4]));

        let searchUrl = request(input).match(regex);
        if(searchUrl) {
            searchUrl = searchUrl[1] + '?top=10&q=' + urlencode(inputSplit[4]);
            let content = request(searchUrl,{headers:headers});
            if (content.charCodeAt(0) === 0xFEFF) {
                content = content.slice(1)
            }
            jsondata = JSON.parse(content);
            jsondata.forEach(function(item) {
                let vod_id = item['url'];
                if(vod_id.startsWith('/')) vod_id = originUrl+vod_id;
                d.push({
                    url:vod_id,
                    title:item['title'],
                    img:item['thumb'],
                    desc:item['lianzaijs'].length>0?item['lianzaijs']+'集':'1集'
                })
            })
        }

        setResult(d)
    `,
}